package de.gameduell.framework.chat {
	import de.gameduell.framework.application.controller.IMultiplayerController;
	import de.gameduell.framework.application.protocol.MultiplayerProtocol;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.game.Player;
	import de.gameduell.framework.locale.Locale;
	import de.gameduell.framework.util.Library;
	import de.gameduell.net.src.communication.ServerMessageEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;	

	/**
	 * @author cbra
	 */
	public class ChatBubble {	

		private var _chatBubble:Sprite;
		private var smiley:Sprite;
		private var chatOutput:TextField;
		protected var locale:Locale;
		private var chatPopupTimer:Timer;
		private var library:Library;
		private var controller:IMultiplayerController;
		protected var player:Player;
		private var smileyScale:Number;
		

		public function ChatBubble(aController:IMultiplayerController,aPlayer:Player,aChatBubble:Sprite,bubbleLifeTime:Number, aSmileyScale:Number = 2) {
			controller = aController;
			player = aPlayer;
			locale = Locale.getInstance();
			library = Library.getInstance();
			chatPopupTimer = new Timer(bubbleLifeTime, 1);
			chatPopupTimer.addEventListener(TimerEvent.TIMER_COMPLETE, hideChatPopup);
			controller.addEventListener(MultiplayerProtocol.CHAT, onChat);
			smileyScale = aSmileyScale;
			createChatBubble(aChatBubble);
		}

		private function onChat(event:ServerMessageEvent):void {
			if (controller.chatActive) {
				if(player.identity == event.parameters["playerId"]) {
					showChatMessage(event.parameters["message"]);
				}
			}
		}

		/**
		 * Display an incomming chatmessage in bubble.
		 * Translates the key into localized Text.
		 * @param aMessage		incomming message to display
		 */
		public function showChatMessage(aKey:String):void {
			_chatBubble.visible = true;
			var localisedKey:String = locale.loadString(aKey);
			var textChatMsg:String = getPlainTextFromLink(localisedKey);
			if (localisedKey == ""){
				chatOutput.text = aKey;
			}
			else if(textChatMsg != localisedKey) {
				clearSprite(smiley);
				chatOutput.text = textChatMsg;
			} else {
				chatOutput.text = "";
				clearSprite(smiley);
				try {
					var smileyPicture:Sprite = library.createSprite("small_" + aKey);
					smileyPicture.scaleX = smileyScale;
					smileyPicture.scaleY = smileyScale;
					smiley.addChild(smileyPicture);
				}catch(e:Error) {
					chatOutput.text = localisedKey;
				}
			}
			chatPopupTimer.stop();
			chatPopupTimer.start();
		}

		public static function getPlainTextFromLink(htmlText:String):String {
			var reg:RegExp = /<a.*>(.*)<\/a>/i;
			var result:Object = reg.exec(htmlText);
			return result == null ? htmlText : result[1];
		}

		public function createChatBubble(aChatBubble:Sprite):void {
			_chatBubble = aChatBubble;
			_chatBubble.addEventListener(MouseEvent.MOUSE_DOWN, hideChatPopup);
			_chatBubble.visible = false;
			_chatBubble.alpha = 1;
			chatOutput = _chatBubble.getChildByName("content") as TextField;
			smiley = _chatBubble.getChildByName("smiley") as Sprite;
		}

		/**
		 * hide chatBubble after CHAT_POPUP_DELAY
		 */
		private function hideChatPopup(event:Event = null):void {
			clearSprite(smiley);
			_chatBubble.visible = false;
		}

		private function clearSprite(aSprite:Sprite):void {
			while(aSprite.numChildren > 0) {
				aSprite.removeChildAt(0);
			}
		}
	}
}
